<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./js/vue.js"></script>
    <style>
        .active {
            background: #ff4400;
            color: #fff;
        }
    </style>
</head>

<body>
    <div id="app">
        <input type="text" v-model="kw" @keydown.down="down" @keydown.up.prevent="up" @keydown.enter="enter">
        <ul>
            <li v-for="(item,index) in arr" v-if="4>index" :key="item" :class="index===n?'active':''">{{item}}</li>
        </ul>
    </div>
    <script>
        let vm = new Vue({
            el: "#app",
            data: {
                kw: "",
                arr: [],
                n: -1
            },
            watch: {
                kw() {
                    if (this.kw === '') {
                        this.arr = []
                        return;
                    }
                    let os = document.createElement("script")
                    os.src = `http://suggestion.baidu.com/su?cb=callback&wd=` + this.kw;
                    document.body.appendChild(os)
                }
            },
            methods: {
                down() {
                    this.n++;
                    if (this.n >= 4) {
                        this.n = -1
                    }
                },
                up() {
                    this.n--;
                    if (this.n < 0) {
                        this.n = 4
                    }
                },
                enter() {
                    if (this.n >= 0 && this.n <= 3) {
                        window.open("https://www.baidu.com/s?wd=" + this.arr[this.n], "_self")
                    } else {
                        window.open("https://www.baidu.com/s?wd=" + this.kw, "_self")

                    }

                }
            }
        })
        function callback(res) {
            console.log(res);
            vm.arr = res.s
        }
    </script>
</body>

</html>